package com.quad.innovators.salesease.service.impl.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.common.result.PageResultBean;
import com.quad.innovators.salesease.common.utils.SystemCodeUtils;
import com.quad.innovators.salesease.converter.PurchaseOrderConverter;
import com.quad.innovators.salesease.enums.ArrivalStatusEnum;
import com.quad.innovators.salesease.enums.ResultCode;
import com.quad.innovators.salesease.enums.SystemCodeEnum;
import com.quad.innovators.salesease.exception.BusinessException;
import com.quad.innovators.salesease.mapper.warehouse.PurchaseOrderMapper;
import com.quad.innovators.salesease.model.entity.warehouse.ArrivalOrder;
import com.quad.innovators.salesease.model.entity.warehouse.PurchaseOrder;
import com.quad.innovators.salesease.model.form.warehouse.PurchaseOrderForm;
import com.quad.innovators.salesease.model.query.warehouse.PurchaseOrderPageQuery;
import com.quad.innovators.salesease.model.vo.warehouse.PurchaseOrderVO;
import com.quad.innovators.salesease.service.warehouse.PurchaseOrderDetailService;
import com.quad.innovators.salesease.service.warehouse.PurchaseOrderService;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import lombok.RequiredArgsConstructor;

/**
 * @author : shanshan.li
 * @createTime : 2024/10/18 15:25
 * @Version : 1.0 
 */
@Service
@RequiredArgsConstructor
public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, PurchaseOrder> implements PurchaseOrderService {

    private final PurchaseOrderConverter purchaseOrderConverter;
    private final PurchaseOrderDetailService purchaseOrderDetailService;

    @Override
    public PageResultBean<PurchaseOrderVO> listPage(PurchaseOrderPageQuery queryParams) {
        Page<PurchaseOrderVO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize());
        Page<PurchaseOrderVO> warehouseVOPage = this.baseMapper.listPage(page, queryParams);
        return purchaseOrderConverter.toPageVo(warehouseVOPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(PurchaseOrderForm form) {
        if (CollectionUtils.isEmpty(form.getPurchaseOrderDetailFormList())) {
            throw BusinessException.resultCode(ResultCode.COLLECTION_NOT_EMPTY);
        }
        PurchaseOrder entity = purchaseOrderConverter.toEntity(form);
        LocalDate currentTime = LocalDate.now();
        String formatCurrent = currentTime.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        PurchaseOrder purchaseOrder = this.getOne(new QueryWrapper<PurchaseOrder>()
                .lambda()
                .likeRight(PurchaseOrder::getOrderNumber, formatCurrent)
                .orderByDesc(PurchaseOrder::getOrderNumber)
                .last("limit 1"));
        if (ObjectUtils.isEmpty(purchaseOrder)) {
            entity.setOrderNumber(formatCurrent + "00001");
        } else {
            entity.setOrderNumber(String.valueOf(Long.parseLong(purchaseOrder.getOrderNumber()) + 1));
        }
        entity.setCode(SystemCodeUtils.getCode(SystemCodeEnum.PURCHASE_ORDER));
        entity.setArrivalStatus(ArrivalStatusEnum.NOT.getCode());
        return this.baseMapper.insert(entity) > 0
                && purchaseOrderDetailService.saveList(entity.getCode(), form.getPurchaseOrderDetailFormList());
    }

    @Override
    public PurchaseOrderForm getFormData(String code) {
        PurchaseOrder purchaseOrder = this.baseMapper.selectOne(new QueryWrapper<PurchaseOrder>().lambda().eq(PurchaseOrder::getCode, code));
        if (ObjectUtils.isEmpty(purchaseOrder)) {
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        PurchaseOrderForm purchaseOrderForm = purchaseOrderConverter.toForm(purchaseOrder);
        purchaseOrderForm.setPurchaseOrderDetailFormList(purchaseOrderDetailService.getListByPurchaseOrderCode(code));
        return purchaseOrderForm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(String code, PurchaseOrderForm form) {
        if (CollectionUtils.isEmpty(form.getPurchaseOrderDetailFormList())) {
            throw BusinessException.resultCode(ResultCode.COLLECTION_NOT_EMPTY);
        }
        PurchaseOrder purchaseOrder = this.baseMapper.selectOne(new QueryWrapper<PurchaseOrder>().lambda().eq(PurchaseOrder::getCode, code));
        if (ObjectUtils.isEmpty(purchaseOrder)) {
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        PurchaseOrder entity = purchaseOrderConverter.toEntity(form);
        entity.setId(purchaseOrder.getId());
        return this.baseMapper.updateById(entity) > 0
                && purchaseOrderDetailService.updateList(code, form.getPurchaseOrderDetailFormList());
    }
}
